var utils = require('../../utils/util.js');
var httpUrl = getApp().globalData.httpUrl;
var codeName = getApp().globalData.codeName;
var userId = getApp().globalData.userId;
Page({
  data: {
    ishis: false, //用户回复是否弹出键盘
    isSubmit: false, //键盘输入触发“发送”按钮变色
    isuser: false
  },
  onLoad: function (options) {
    var that = this;
    wx.setNavigationBarTitle({
      title: options.typename,
    })
    that.setData({
      cityid: options.cityid,
      modelid: options.id
    })
    that.getlodeTap();

  },

  getlodeTap: function (e) {
    var that = this;
    utils.http(httpUrl + "getCateList", { 
      code: codeName, userid: userId, cityid: that.data.cityid,
       modelid: that.data.modelid
       }, function (res) {
      console.log(res);
      var cateInfoLists = [];
      for (var index in res.catainfolist) {
        if (res.catainfolist[index].content == undefined) {
          res.catainfolist[index].content = "";
        }
        //文字截取
        var sub = res.catainfolist[index].content.substring(0, 40);
        var subContent = sub + "...";
        that.setData({
          advlist: res.catainfolist[index].advlist
        })
        that.data.advlist.length = 3;
        // res.catainfolist[index].collect = !res.catainfolist[index].collect;
        var catePost = {
          advlist: that.data.advlist,
          subAdvlist: res.catainfolist[index].advlist, //截取后的图片
          collectby: res.catainfolist[index].collectby,
          collect: res.catainfolist[index].collect,
          content: res.catainfolist[index].content,
          subContent: subContent,//截取后的属性
          createTime: res.catainfolist[index].createTime,
          id: res.catainfolist[index].id,
          index: res.catainfolist[index].index,
          ischoiceness: res.catainfolist[index].ischoiceness,
          istop: res.catainfolist[index].istop,
          lookby: res.catainfolist[index].lookby,
          publishby: res.catainfolist[index].publishby,
          title: res.catainfolist[index].title,
          type: res.catainfolist[index].type,
          user: res.catainfolist[index].user,
          isstare: false,
          commentlist: res.catainfolist[index].commentlist,
        }
        cateInfoLists.push(catePost);

      }

      that.setData({
        bannerList: that.data.advlist,
        cateinfo: cateInfoLists
      })
      //  console.log(that.data.cateinfo);
    })
  },

  //全文和收起
  noStareTap: function (e) {
    var index = e.currentTarget.dataset.index;
    this.data.cateinfo[index].isstare = !this.data.cateinfo[index].isstare;
    this.setData({
      cateinfo: this.data.cateinfo
    })
  },


  //收藏切换
  oNcollectbyTap: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    // that.data.cateinfo[index].collect = !that.data.cateinfo[index].collect;
    console.log(that.data.cateinfo[index].collect);
    var collectby = "";
    if (that.data.cateinfo[index].collect == false) {
      console.log(that.data.cateinfo[index].collect + "收藏");
      collectby = parseInt(that.data.cateinfo[index].collectby) + 1;
      that.data.cateinfo[index].collectby = collectby;
      utils.http(httpUrl + "manageCollect", {
        code: codeName,
        cateid: that.data.cateinfo[index].id,
        userid: userId
      }, function (res) {
        that.setData({
          cateinfo: that.data.cateinfo
        })
        that.getlodeTap();
      })
    } else {
      console.log(that.data.cateinfo[index].collect + "取消收藏");
      collectby = parseInt(that.data.cateinfo[index].collectby) - 1;
      that.data.cateinfo[index].collectby = collectby;
      utils.http(httpUrl + "manageCollect", {
        code: codeName,
        cateid: that.data.cateinfo[index].id,
        userid: userId
      }, function (res) {
        that.setData({
          cateinfo: that.data.cateinfo
        })
        that.getlodeTap();
      })
    }
  },


  //点击头像进入个人主页
  onPersonalHomeTap: function (e) {
    var modelid = e.currentTarget.dataset.modelid;
    wx.navigateTo({
      url: '../gourmet/gourmet_details/gourmet_details?modelid=' + modelid,
    })
  },

  //联系他
  onPhoneTap: function (e) {
    var phone = e.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: phone,
    });
  },

  //键盘输入是触发按钮
  oNbindinputTap: function (e) {
    var valueName = e.detail.value;
    if (valueName.length == 0) {
      this.data.isSubmit = false;
    } else {
      this.data.isSubmit = true;
    }
    this.setData({
      isSubmit: this.data.isSubmit,
      valueName: valueName
    })

  },

  //点击回复信息小图标
  oNinfonumTap: function (e) {
    var cateid = e.currentTarget.dataset.cateid;
    var nickname = e.currentTarget.dataset.nickname;
    var type = e.currentTarget.dataset.type;
    this.setData({
      ishis: true,
      cateid: cateid,
      nickname: nickname,
      type: type,
      valueName:"",
      username:"",
    })
  },

  //点击不同用户回复信息
  onchangeTap: function (e) {
    var that = this;
    var cateid = e.currentTarget.dataset.cateid;
    var parentusername = e.currentTarget.dataset.parentusername;
    var nickname = e.currentTarget.dataset.nickname;
    var parentid = e.currentTarget.dataset.parentid;
    var parentuserid = e.currentTarget.dataset.parentuserid;
    var username = e.currentTarget.dataset.username;   //要回复的username
    var type = e.currentTarget.dataset.type;
    var userid = e.currentTarget.dataset.userid;
    var id = e.currentTarget.dataset.id;
    if (parentuserid == undefined) {
      that.setData({
        parentid: id,
        parentuserid: userid,
      })
    } else {
      that.setData({
        parentid: parentid,
        parentuserid: parentuserid,
      })
    }

    this.setData({
      ishis: true,
      cateid: cateid,
      parentusername: parentusername,
      nickname: nickname,
      type: type,
      username: username,
      valueName:""
    })
  },

  //提交回复
  bindsubmit: function (e) {
    var that = this;
    if(that.data.type==1){
       utils.http(httpUrl +"publishComment",{
        userid: userId, //	用户id
        cateid: that.data.cateid,//美食id
        code: codeName,//code值
        content: that.data.valueName, //	评论内容
        username: that.data.nickname  // 发布的用户的名字
      },function(res){
          console.log(res);
          that.getlodeTap();
      })
    }else{
      utils.http(httpUrl + "publishComment", {
      userid: userId, //	用户id
      cateid: that.data.cateid,//美食id
      code: codeName,//code值
      content: that.data.valueName, //	评论内容
      username: that.data.nickname,// 发布的用户的名字
      parentid: that.data.parentid,  //评论的ID
      parentuserid: that.data.parentuserid,
      parentusername: that.data.username
    }, function (res) {
      console.log(res);
      that.getlodeTap();
    })
    }

   


   
  },
  handletouchmove:function(e){
    this.setData({
      ishis: false,
    })
  }

})